package com.poc.train.pr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.Expose;
import com.poc.train.entity.PocDept;

/**
 * 
 * @author Andy
 *
 */
@Component
public class PocDeptPR extends HibernateDao {

	/**
	 * pocDeptPR#getRootData 获取最高级别的部门信息
	 * 
	 * @param parentId
	 * @return
	 */
	@DataProvider
	public List<PocDept> getRootData() {
		return getDeptByPid(PocDept.DEFAULT_PARENT);
	}

	/**
	 * pocDeptPR#getDeptByPid 根据父节点获取子部门列表
	 * 
	 * @param parentId
	 * @return
	 */
	@DataProvider
	public List<PocDept> getDeptByPid(String parentId) {

		String hql = "from " + PocDept.class.getName()
				+ " where parentId=:parentId";

		Map<String, Object> args = new HashMap<String, Object>();
		args.put("parentId", parentId);

		return this.query(hql, args);
	}

	/**
	 * pocDeptPR#saveDept
	 * 
	 * @param dept
	 * @return
	 */
	@Expose
	public String saveDept(PocDept dept) {

		Session session = this.getSessionFactory().openSession();
		try {
			if (null == dept.getParentId()) {
				dept.setParentId(PocDept.DEFAULT_PARENT);
			}
			session.saveOrUpdate(dept);
		} finally {
			session.flush();
			session.close();
		}

		return null;
	}

	/**
	 * 删除数据
	 * 
	 * @param dept
	 * @return
	 */
	@Expose
	public String delDept(PocDept dept) {
		Session session = this.getSessionFactory().openSession();
		try {
			session.delete(dept);
		} finally {
			session.flush();
			session.close();
		}
		return null;
	}
}
